package com.cus.tom.transfer.ac;

import android.content.Intent;
import android.os.Bundle;
import android.util.SparseIntArray;
import android.view.View;

import androidx.annotation.Nullable;

import com.cus.tom.transfer.R;
import com.cus.tom.transfer.bean.Const;
import com.cus.tom.transfer.bean.TypeFile;
import com.cus.tom.transfer.databinding.ActOverBinding;
import com.cus.tom.transfer.databinding.OverItemBinding;
import com.cus.tom.transfer.db.RecvDao;
import com.cus.tom.transfer.db.SendDao;
import com.cus.tom.transfer.db.TransDao;
import com.cus.tom.transfer.utils.ToastUtil;
import com.cus.tom.transfer.utils.Utils;
import com.plata.base.aclr.common.ITask;
import com.plata.base.aclr.common.SilentTask;
import com.plata.base.aclr.common.TaskEngine;

import java.io.File;
import java.util.List;

public class OverAct extends BaseAct {

    ActOverBinding binding;
    ITask task = null;
    int type;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActOverBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        type = getIntent().getIntExtra("type", 0);//0:发送；1：接收
        String id = getIntent().getStringExtra("id");
        if (type == 0) {
            binding.back.setText("发送完成");
        } else {
            binding.back.setText("接收完成");
            check(new File(Const.SAVE_DIR));
        }
        getData(id, type);

        binding.back.setOnClickListener(v -> finish());
        binding.btn.setOnClickListener(v -> finish());

        if (type == 0) {
            sendBroadcast(new Intent(Const.EXIT_RESULT));
        }
    }

    private void getData(final String id, final int type) {
        task = new SilentTask("") {
            @Override
            public void runInBackground() throws Exception {
                TransDao dao;
                if (type == 0) {
                    dao = new SendDao();
                } else {
                    dao = new RecvDao();
                }
                List<TypeFile> list = dao.getAllById(id);
                h.post(() -> {
                    showResult(true, null, list);
                });
            }
        };
        TaskEngine.getInstance().submit(task);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (task != null) {
            task.cancel();
        }
    }

    private void showResult(boolean bo, String msg, Object result) {
        binding.bar.setVisibility(View.GONE);
        if (!bo) {
            ToastUtil.showShortToast(this, msg);
            return;
        }
        if (result instanceof List == false) {
            return;
        }
        List<TypeFile> list = (List<TypeFile>) result;
        if (type == 0) {
            binding.tv.setText(String.format("成功发送%d个文件", list.size()));
        } else {
            binding.tv.setText(String.format("成功接收%d个文件", list.size()));
        }
        SparseIntArray array = new SparseIntArray();
        for (TypeFile f : list) {
            int i = f.type.getCode();
            int v = array.get(i, 0);
            array.put(i, ++v);
        }
        String[] ss = getResources().getStringArray(R.array.title);
        for (int i = 0; i < ss.length; i++) {
            int v = array.get(i, 0);
            if (v > 0) {
                OverItemBinding b = OverItemBinding.inflate(getLayoutInflater(), binding.linear, true);
                b.title.setText(ss[i]);
                b.num.setText(v + "");
            }
        }
    }

    private void check(File file) {
        if (file.isDirectory()) {
            File[] fs = file.listFiles();
            if (fs != null) {
                for (File f : fs) {
                    check(f);
                }
            }
        } else if (file.getName().endsWith(".tp")) {
            Utils.deleteFile(file);
        }
    }
}
